package nekocalc.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class AboutDialog extends JDialog implements MouseListener{
	private final boolean DEBUG = false;
	private int width;
	private int height;
	
	public AboutDialog() {
		width = 480;
		height = 360;
		setMinimumSize(new Dimension(width, height));
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setUndecorated(true);
		setBackground(new Color(0, 0, 0, 15));
		setContentPane(new AboutPanel());

		setModal(true);
		addMouseListener(this);
		centerAlignDialog();

		
		pack();
		//repaint();
		display();
		
	}
	
	class AboutPanel extends JPanel{
		private String strVersion = "0.0a";
		public AboutPanel() {
			// TODO Auto-generated constructor stub
		}

		protected void paintComponent(Graphics g) {
						
			if (g instanceof Graphics2D) {
				//*
				final int R = 240;
				final int G = 240;
				final int B = 240;
				ImageIcon icon = new ImageIcon(AboutDialog.class.getResource("../res/img/AboutIcon.png"));
				Image img = icon.getImage();
				final int fontSmallerSize = 12;
				final int fontPlainSize = 14;
				Color bgColor = new Color(50, 50, 50, 240);
				Color fgColor = new Color(240, 240, 240, 100);
				String string = "猫電卓 - NekoCalc v."+strVersion;
				String disclaimerL1 = "Under GPL 3.0 license.\n";
				String disclaimerL2 = "Developped by EminoMeneko. Usage of this software is free.";
			
				Paint p = new GradientPaint(
						0.0f, 0.0f, new Color(R,G,B,0), 
						0.0f, getHeight(),new Color(R,G,B,0), true); ///*/
				
				
				Graphics2D  g2d = (Graphics2D)g;
				g2d.setPaint(p);
				g2d.fillRect(0, 0, getWidth(), getHeight());
				g2d.drawImage(img, 0, 0, new Color(0,0,0,0),null);
				
				//Drawing String Containers
				g2d.setColor(fgColor);
				g2d.fillRoundRect(235, 85, 200, 20, 15, 15);
				g2d.fillRoundRect(25, 305, 415, 30, 15, 15);
				
				// Drawing some strings
				Font font = new Font(Font.MONOSPACED, Font.BOLD, fontPlainSize);
				g2d.setFont(font);
				g2d.setColor(bgColor);
				g2d.drawString(string, 240, 100);
				font = new Font(Font.MONOSPACED, Font.BOLD, fontSmallerSize);
				g2d.setFont(font);
				g2d.setColor(bgColor);
				g2d.drawString(disclaimerL1, 30, 320);
				g2d.drawString(disclaimerL2, 30, 330);				
			}
			
		}
	}
	
	private void centerAlignDialog() {
		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension dialogDim = this.getSize();
		int posX = (screenDim.width - dialogDim.width)/2;
		int posY = (screenDim.height - dialogDim.height)/2;
		
		if (DEBUG) {
			System.out.println("AboutDialog: centerAlignDialog(): ");
			System.out.println("Screen size: "+ new Integer(screenDim.width).toString()
					+" × "
					+ new Integer(screenDim.height).toString()+".");
			System.out.println("Dialog size: "+ new Integer(dialogDim.width).toString()
					+" × "
					+ new Integer(dialogDim.height).toString()+".");
			System.out.println("Final coordinate: ("
					+ new Integer(posX).toString()+", "
					+ new Integer(posY).toString()+")");
		}
		setLocation(posX, posY);
	}

	public void display() {
		this.setVisible(true);
	}
	public void close() {
		this.setVisible(false);
		this.dispose();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (DEBUG) {System.out.println("AboutDialog : Mouse click performed.");}
		close();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
